import Vue from 'vue'
import Toast from '@/components/Toast/index.vue'

const ToastConstructor = Vue.extend(Toast)
let instance

function createInstance() {
  instance = new ToastConstructor({
    el: document.createElement('div')
  })
  document.body.appendChild(instance.$el)
}

function toast(message, type) {
  if (!instance) {
    createInstance()
  }
  instance.show(message, type)
}

export default {
  install(Vue) {
    Vue.prototype.$message = {
      success(message) {
        toast(message, 'success')
      },
      error(message) {
        toast(message, 'error')
      },
      warning(message) {
        toast(message, 'warning')
      },
      info(message) {
        toast(message, 'info')
      }
    }
  }
} 